﻿using Microsoft.Owin.Hosting;
using System;
using System.Configuration.Abstractions;

namespace Sustainalytics.ClientInterface.AngularHosting.Service
{
    public class ServiceHost
    {
        // Create the variable for our self-hosted server
        //
        private IDisposable _server;

        // Just hard-code the address for now
        //
        private string _baseAddress = ConfigurationManager.Instance.AppSettings.AppSettingSilent<string>("HostUrl", () => "http://locahost:8080");

        public ServiceHost()
        {
            Console.WriteLine("ServiceHost constructed");
        }

        public void Start()
        {
            Console.WriteLine("ServiceHost started");

            _server = WebApp.Start<Startup>(url: _baseAddress);

            Console.WriteLine($"Server running at {_baseAddress}");
        }

        public void Shutdown()
        {
            Console.WriteLine("ServiceHost shutting down");
        }

        public void Stop()
        {
            Console.WriteLine("ServiceHost stopped");

            _server.Dispose();

            Console.WriteLine("ServiceHost stopped");
        }
    }
}